import React,{Component} from 'react';
import{
  ToastAndroid,
} from 'react-native';
import SQLiteStorage from 'react-native-sqlite-storage';
SQLiteStorage.DEBUG(true);
var database_name = "draft.db";
var database_version = "1.0";
var database_displayname = "MySQLite";
var database_size = -1;
var db;
const ACCOUNT_TABLE = "t_account";//用户表
export default class  SQLite extends Component{
	componentWillUnmount(){
    if(db){
        this._successCB('close');
        db.close();
    }else {
        console.log("SQLiteStorage not open");
    }
  }

  open(){

    db = SQLiteStorage.openDatabase(
      database_name,
      database_version,
      database_displayname,
      database_size,
      ()=>{
          this._successCB('open');

      },
      (err)=>{
          this._errorCB('open',err);
      });
    //this.dropTable();
    //this.dropTable();
    //this.dropImgTable();
    
    return db;
  }
  createTable(){
    //this.dropTable();
    db = SQLiteStorage.openDatabase(
      database_name,
      database_version,
      database_displayname,
      database_size,
      ()=>{
          this._successCB('open');
      },
      (err)=>{
          this._errorCB('open',err);
    });
    //创建表
    db.transaction((tx)=> {
      // val1:'5', //成本
      // val2:'10', // 单价
      // val3:'0', //宽粉 重量
      // val4:'0',  //宽粉 金额
      // val5:'0',  //细粉 重量
      // val6:'0',  //细粉  金额
      // val7:'0',  //赠送 重量
      // totalFund:'0',
      // totalCost:'0',
      // totalIncome:'0',
      // val8:'0',
      tx.executeSql('CREATE TABLE IF NOT EXISTS ' + ACCOUNT_TABLE + '(' +
          'id INTEGER PRIMARY KEY  AUTOINCREMENT,' +
          'val1 varchar,'+
          'val2 VARCHAR, '+
          'val3 varchar,'+
          'val4 varchar,'+
          'val5 varchar,'+
          'val6 varchar,'+
          'val7 varchar,'+
          'val8 varchar,'+
          'total_fund varchar,'+
          'total_cost varchar,'+
          'total_income varchar,'+
          'create_time varchar'+
          ');'
          , [], ()=> {
              this._successCB('executeSql  create tablee');
          }, (err)=> {
              this._errorCB('executeSql', err);
        });
    }, (err)=> {
        this._errorCB('transaction', err);
    }, ()=> {
        this._successCB('transaction');
    })
	}
	errorCB(err) {
	  console.log("SQL Error: " + err);
	  console.log(err);
	}
	openCB() {
	  console.log("Database OPENED");
	  console.log('open');
	}
	
  deleteData(){
    db.transaction((tx)=>{
      tx.executeSql('delete from '+NOTE_TABLE,[],()=>{

      });
    });
  }
  deleteImgData(){
    db.transaction((tx)=>{
      tx.executeSql('delete from '+IMG_TABLE,[],()=>{

      });
    });
  }
  close(){
      if(db){
          this._successCB('close');
          db.close();
      }else {
          console.log("SQLiteStorage not open");
      }
      db = null;
  }
  _successCB(name){
    console.log("SQLiteStorage "+name+" success");
  }
  _errorCB(name, err){
    console.log("SQLiteStorage "+name+" error:"+err);
  }
	render(){
		return null;
	}
};
